package com.wangp.mywrite.inwork.pattern._4._4_01_bold;


/***
 * @author wangpeng
 * @since 2022-03-01  16:14
 */
public class Bold {

    public static void main(String[] args) {
        String sentence = "This cat scattered his food all over the room.";

        // 需要替换 cat 为dog
        String catReg = "cat";
        String newStr = "dog";
        // This dog sdogtered his food all over the room. 其中 scattered 被替换成了 sdogtered.
        System.out.println(sentence.replaceAll(catReg, newStr));

        // 使用单词边界来控制
        String boldReg = "\\bcat\\b";
        // This dog scattered his food all over the room.
        System.out.println(sentence.replaceAll(boldReg, newStr));

        String instance = "The captain wore his cap and cape proudly as he sat listening to the recap of how his crew saved the men from a capsized vessel.";
        String boldRegex = "\\bcap\\b";
        String leftBoldReg = "\\bcap";
        String rightBoldReg = "cap\\b";
        // The captain wore his - and cape proudly as he sat listening to the recap of how his crew saved the men from a capsized vessel.
        System.out.println(instance.replaceAll(boldRegex, "-"));
        // The -tain wore his - and -e proudly as he sat listening to the recap of how his crew saved the men from a -sized vessel.
        System.out.println(instance.replaceAll(leftBoldReg, "-"));
        // The captain wore his - and cape proudly as he sat listening to the re- of how his crew saved the men from a capsized vessel.
        System.out.println(instance.replaceAll(rightBoldReg, "-"));

        // \b 匹配边界:单词（即字母数字下划线）边界，对应的 \B 对应非单词边界。
        // 注意这里重点是边界，因为跟 \b 或者 \B 紧接着的字符有关，若紧接的字符是单词字符，那么边界就是非单词字符。
        String sentence2 = "Please enter the nine-digit id as it appears on your color - coded pass-key.";
        // 这里中划线不是单词字符，因此他的边界是单词字符。而 \B 是找非边界字符。
        // 对一个第一个中划线位置  nine-digit，- 左边是单词字符e是边界字符，因为不匹配。
        // 对于第二个 color - coded ，- 左边是空格字符是非单词字符，也就不是边界，同理右边也不是边界，因此匹配
        // 对于第三个 pass-key 同第一个。
        String regBold = "\\B-\\B";
        // Please enter the nine-digit id as it appears on your color = coded pass-key.
        System.out.println(sentence2.replaceAll(regBold, "="));

        String bold = "\\b-\\b";
        // Please enter the nine=digit id as it appears on your color - coded pass=key.
        System.out.println(sentence2.replaceAll(bold, "="));

        // 想匹配如下字符的第一个-,如何匹配？
        // -Please 对于字符 - 是开头但不是\w ，因此不认为是边界。
        String sentence3 = "-Please enter the nine-digit id as it appears on your color - coded pass-key.";
        String myBold = "\\B-\\b";
        System.out.println(sentence3.replaceAll(myBold, "="));

        // a 对于字符 a 是开头并且是\w ，因此认为是边界。
        String sentence4 = "a cat";
        String myBold2 = "\\ba\\b";
        System.out.println(sentence4.replaceAll(myBold2, "="));
    }

    /**
     * 有三种不同的位置可作为词边界：
     *
     * <p>在字符串开头，如果第一个字符是单词字符 \w。</p>
     * <p>在字符串中的两个字符之间，其中一个是单词字符 \w，另一个不是。</p>
     * <p>在字符串末尾，如果最后一个字符是单词字符 \w。</p>
     */
    private void lookNote() {
    }
}
